package gwt.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import gwt.client.rpc.MFService;
import gwt.server.util.BaseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by swootton on 02/04/14.
 *
 * It is not possible to call this version of the RPC directly from the client.
 * You must create an asynchronous interface to all your services as shown below.
 *
 * RemoteServiceServlet extends javax.servlet.http.HttpServlet
 *
 */


public class MFServiceImpl extends BaseServiceImpl implements MFService {

    private static final long serialVersionUID = -947387287029228608L;

    public MFServiceImpl() {

    }

    /**
     * doPost(HttpServletRequest, HttpServletResponse)' in 'gwt.server.MFServiceImpl'
     * clashes with 'doPost(HttpServletRequest, HttpServletResponse)' in
     * 'com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet';
     * overridden method does not throw 'javax.servlet.ServletException'
     *
     * @throws ServletException
     * @throws IOException
     *
     * could just have a single method (doMFPost()), as whole request is passed
     * on to MFServer, no need to interface RPC calls per client
     * functionality?
     */

    @Override
    public String getRepositories(String requestXML) /*throws ServletException, IOException*/ {

        System.out.println("MFServiceImpl.getRepositories()");
        System.out.println(requestXML);

        //HttpServletRequest request = getThreadLocalRequest();
        //HttpServletResponse response = getThreadLocalResponse();

        // http://samwootton.com:8080/mfservice/music
        //RequestDispatcher requestDispatcher = request.getRequestDispatcher("/music");
        //System.out.println("requestDispatcher "+ requestDispatcher);

        // TODO change to whole request object (request) instead?
        // TODO already implicit / grouped by requestDispatcher.forward() ?
        // TODO fwd to Servlet in different webapp (MFClient app is different from MFServer)
        //request.setAttribute("request_xml", requestXML);
        /*
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("../server.properties");

        System.out.println("inputStream "+inputStream.toString());

        Properties props = new Properties();
        try {
            props.load(inputStream);
        } catch (IOException e) {
            System.out.println("MFServiceImpl IOException" + e.getMessage());
            e.printStackTrace();
        }

        String db_url = props.getProperty("db_url");
        */
        /*
        try {
            requestDispatcher.forward(request, response);
        } catch (ServletException e) {
            System.out.println("ServletException" + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IOException" + e.getMessage());
            e.printStackTrace();
        }
        */
        return "Helloworld from MFServiceImpl";
    }
}

/**
 public abstract class BaseServiceImpl extends RemoteServiceServlet implements Controller, ServletContextAware {

 private static final long serialVersionUID = -947387287029228608L;

 private ServletContext servletContext;


 @Override
 public final ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception { // NOSONAR
 doPost(req, resp);
 return null;
 }


 public ServletContext getServletContext() {
 return servletContext;
 }


 public void setServletContext(ServletContext servletContext) {
 this.servletContext = servletContext;
 }
 }
 */

